//
//  AutoSQLiteSwiftViewController.swift
//  SQLiteDemo
//
//  Created by lete on 2020/6/9.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit

//import AutoSQLiteSwift

class AutoSQLiteSwiftViewController: UIViewController {

    // 创建db
    lazy var manager: SQLiteDataBase = {
        return SQLiteDataBase.createDB("statementDB")
    }()

    // 测试的model
    lazy var testModel: TestModel = {
        let testModel =  TestModel()
        testModel.age       = 18
        testModel.name      = "Tony"
        testModel.ignore    = "ignore"
        testModel.weight    = 140
        testModel.newAge    = 19
        testModel.uuid      = "testuuid1"
        
        testModel.optionalInt = 1
        testModel.optionalFloat = 2.0
        testModel.optionalDouble = 3.0
        testModel.optionalisTest = true
        testModel.optionalString = "optionalString"
        
//        testModel.MyTestModel = [MyTestModel]()
//        for item in 0...2{
//          let mymodel =  MyTestModel()
//            mymodel.myname = "liu"
//            mymodel.mytest = "3453535"
//            testModel.MyTestModel?.append(mymodel)
//        }
        
        return testModel;
    }()
    
    var updateIndex = 1
    let sqlTableName = "testTable"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
    }
    
    @IBAction func insertAction() {
           testModel.name = "Tony"
           
           manager.insert(testModel , intoTable: sqlTableName)
       }
       
       
       @IBAction func updateAction() {
           // 存在uuid，如果有数据就更新数据，没有数据insert
           testModel.name = "Reet\(updateIndex)"
           testModel.optionalString = "updateOptionalString"
           
           manager.update(testModel, fromTable: sqlTableName)
           updateIndex += 1
       }
       
       
       @IBAction func deleteAction() {
           manager.delete(testModel, fromTable: sqlTableName)
       }
       
       
       @IBAction func selectAction() {
           // select testModel
        let results = manager.select(testModel, fromTable: sqlTableName)
        if results!.count == 0 {
          
               print("没有查询到数据")
               return
        }
        else {
           
            print("查询到数据:\(String(describing: results))")
        }
       }
       
       
       @IBAction func dropAction() {
           manager.drop(dropTable: sqlTableName)
       }
       
       @IBAction func dismissAction(_ sender: Any) {
           dismiss(animated: true, completion: nil)
       }

}
